#include "CASE.h"

#if CASE_SCANF == 1
// None,SCANF_read,1,Expect,offset|len|timeout|readlen|sendbuf
CASE(SCANF_read)
{
    uint8_t read_buf[32];
    uint8_t send_buf[16];
    uint8_t check_buf[16];
    int32_t offset;
    int32_t len;
    int32_t timeout;
    int32_t readLen;
    caseParam_t offset_param, len_param, timeout_param, readLen_p;
    caseParam_t send_param, check_param;
    memset(send_buf, 0, sizeof(send_buf));
    memset(read_buf, 0, sizeof(read_buf));
    memset(check_buf, 0, sizeof(check_buf));
    // 解析参数
    offset_param.type = Number;
    offset_param.param = &offset;
    len_param.type = Number;
    len_param.param = &len;
    timeout_param.type = Number;
    timeout_param.param = &timeout;
    send_param.type = Bytes;
    send_param.len = 0;
    send_param.param = send_buf;
    readLen_p.type = Number;
    readLen_p.param = &readLen;
    CASE_readParam(data, 5, &offset_param, &len_param, &timeout_param, &readLen_p, &send_param);
    CASE_log("SCANF_read offset:%ld len:%ld timeout:%ld", offset, len, timeout);
    CASE_log_mem("SCANF_read send:", send_buf, send_param.len);
    // 解析数据
    check_param.type = Bytes;
    check_param.len = 0;
    check_param.param = check_buf;
    CASE_readParam(Expect, 1, &check_param);
    CASE_log_mem("SCANF_read check:", check_buf, check_param.len);
    // 执行
    D_log("SCANF_read");
    QUART_receive(read_buf, 100, 0);
    QUART_send(send_buf, send_param.len);
    // len = QUART_receive(read_buf, readLen, timeout);
    if (QUART_receive(read_buf, readLen, timeout) == 0)
    {
        return error;
    }
    char *p = (char *)read_buf + 6;
    // while (*p != ',')
    // {
    //     p++;
    // }
    // p++;
    sprintf(msg, "%s", p);
    CASE_log_mem("SCANF_read read:", read_buf, len);
    // 判断是否相等
    for (int i = 0; i < len; i++)
    {
        if (p[offset + i] != check_buf[i])
        {
            return error;
        }
    }
    return pass;
}
#endif

#if CASE_PRINT == 1

char label1[24] = {0, 0}; // 零件号
char label2[24] = {0, 0}; // 软件版本
char label3[24] = {0, 0}; // 硬件版本
char label4[32] = {0, 0}; // 名称
char label5[24] = {0, 0}; // 供应商代码

char *label_mut[] = {label1, label2, label3, label4, label5};

CASE(Print_set_str)
{
    int32_t index = 0;
    caseParam_t index_param;
    index_param.type = Number;
    index_param.param = &index;
    CASE_readParam(Expect, 1, &index_param);
    if (index >= 5)
    {
        return error;
    }
    strcpy(label_mut[index], data);
    return pass;
}

CASE(Print_set_hex)
{
    int32_t index = 0;
    uint8_t readdata[32];
    caseParam_t index_param, data_param;
    index_param.type = Number;
    index_param.param = &index;
    CASE_readParam(Expect, 1, &index_param);
    if (index >= 5)
    {
        return error;
    }
    data_param.type = Bytes;
    data_param.len = 0;
    data_param.param = readdata;
    CASE_readParam(data, 1, &data_param);
    memcpy(label_mut[index], readdata, data_param.len);
    return pass;
}

const char *tmp1 = "I8,U,001\r\n\
Q236,35\r\n\
q472\r\n\
S40\r\n\
H10\r\n\
ZB\r\n\
R0,0\r\n\
N\r\n\
b363,67,QR,0,0,o0,r4,m4,g1,s8,\"";

const char *tmp2 = "（QC.02）\"\r\n\
A31,34,0,6,1,1,N,\"产品名称：\"\r\n\
A30,96,0,6,1,1,N,\"版本号：\"\r\n\
A30,67,0,6,1,1,N,\"零件号：\"\r\n\
A29,129,0,6,1,1,N,\"供应商代码：\"\r\n\
A30,163,0,6,1,1,N,\"批号：\"\r\n\
A95,163,0,6,1,1,N,\"";
const char *tmp3 = "（QC.02）\"\r\n\
A116,67,0,6,1,1,N,\"";
const char *tmp4 = "\"\r\n\
A115,96,0,6,1,1,N,\"HW：\"\r\n\
A218,96,0,6,1,1,N,\"SW：\"\r\n\
A252,96,0,6,1,1,N,\"";
const char *tmp5 = "\"\r\n\
A149,96,0,6,1,1,N,\"";
const char *tmp6 = "\"\r\n\
A139,34,0,6,1,1,N,\"";
const char *tmp7 = "\"\r\n\
A162,129,0,6,1,1,N,\"";
const char *tmp8 = "\"\r\n\
W1,1\r\n\r\n";
// const char *tmp2 = "SCANF_print";

CASE(SCANF_print)
{
    uint8_t tmp[12] = {0xEF, 0xBB, 0xBF};
    char sn[10];
    if (TestingResult_get() != pass)
    {
        return error;
    }
    CASE_log("SCANF_print");
    QUART_send(tmp, 3);
    sprintf((char *)tmp, "%04d%02d%02d", get_year(), get_month(), get_day());
    sprintf(sn, "%04ld", Get_SN());
    QUART_send((uint8_t *)tmp1, strlen(tmp1));                 // 第一部分
    QUART_send((uint8_t *)label_mut[0], strlen(label_mut[0])); // 零件号
    QUART_send(tmp, 8);                                        // 日期
    QUART_send((uint8_t *)sn, 4);                              // 序列号
    QUART_send((uint8_t *)tmp2, strlen(tmp2));                 // 第二部分
    QUART_send(tmp, 8);                                        // 日期
    QUART_send((uint8_t *)sn, 4);                              // 序列号
    QUART_send((uint8_t *)tmp3, strlen(tmp3));                 // 第三部分
    QUART_send((uint8_t *)label_mut[0], strlen(label_mut[0])); // 零件号
    QUART_send((uint8_t *)tmp4, strlen(tmp4));                 // 第四部分
    QUART_send((uint8_t *)label_mut[1], strlen(label_mut[1])); // 软件版本
    QUART_send((uint8_t *)tmp5, strlen(tmp5));                 // 第五部分
    QUART_send((uint8_t *)label_mut[2], strlen(label_mut[2])); // 硬件版本
    QUART_send((uint8_t *)tmp6, strlen(tmp6));                 // 第六部分
    QUART_send((uint8_t *)label_mut[3], strlen(label_mut[3])); // 名称
    QUART_send((uint8_t *)tmp7, strlen(tmp7));                 // 第七部分
    QUART_send((uint8_t *)label_mut[4], strlen(label_mut[4])); // 供应商代码
    QUART_send((uint8_t *)tmp8, strlen(tmp8));
    QUART_receive(tmp, 100, 100);
    CASE_log("print date:%s", tmp);
    CASE_log("print SN:%s", sn);

    // char *_test = "W1,1\r\n\r\n";
    // QUART_send(_test, strlen(_test));
    return pass;
}
#endif
